package com.situ.company.project.service;


import java.util.List;

import com.situ.company.project.dao.ProjectDao;
import com.situ.company.project.model.ProjectModel;

public class ProjectService {
	ProjectDao dao =new ProjectDao();
	
	public String insert(ProjectModel model) {
		ProjectModel mod = selectModel(model);
		if (mod != null) {
			return "E";
		}
		Integer result = dao.insert(model);
		System.out.println(result);
		return String.valueOf(result);
	}
	
	public String delete(ProjectModel model) {
		Integer result = dao.delete(model);
		return String.valueOf(result);
	}
	
	public String update(ProjectModel model) {
		Integer result = dao.update(model);
		return String.valueOf(result);
	}

	/**
	 * 多条件+模糊查询 --一些记录
	 * 
	 * @param model
	 * @return
	 */
	public List<ProjectModel> selectList(ProjectModel model) {
		String code = model.getCode();
		if (code != null) {
			model.setCode("%" + code + "%");
		}
		String name = model.getName();
		if (name != null) {
			model.setName("%" + name + "%");
		}
		return dao.selectList(model);
	}
	
	/**
	 * 根据(逻辑)主键查询出唯一记录，主键不存在返回null
	 * 
	 * @param model
	 * @return
	 */
	public ProjectModel selectModel(ProjectModel model) {
		ProjectModel temp = new ProjectModel();
		temp.setCode(model.getCode());
		List<ProjectModel> list = dao.selectList(temp);
		if (list == null || list.isEmpty()) {
			return null;
		}
		return list.get(0);
	}
	
	/**
	 * 根据多条件的模糊查询出的记录条数
	 * 
	 * @param model
	 * @return
	 */
	public int selectCount(ProjectModel model) {
		ProjectModel temp = new ProjectModel();
		String code = model.getCode();
		temp.setCode(code == null ? "%%" : "%" + code + "%");
		String name = model.getName();
		temp.setName(name == null ? "%%" : "%" + name + "%");
		temp.setPageOn(false);
		return dao.selectCount(temp);
	}
	
}
